<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="订单状态：" prop="status">
            <el-select
              v-model="listQuery.status"
              filterable
              placeholder="请选择订单状态">
              <el-option
                v-for="item in statues"
                :key="item.name"
                :label="item.name"
                :value="item.name">
                <span style="float: left">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="影票状态：" prop="checkStatus">
            <el-select
              v-model="listQuery.checkStatus"
              filterable
              placeholder="请选择影票状态">
              <el-option
                v-for="item in checkStatues"
                :key="item.name"
                :label="item.name"
                :value="item.name">
                <span style="float: left">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排片场次 id：" prop="rowPieceId">
            <el-input
              v-model="listQuery.rowPieceId"
              placeholder="请输入排片场次id"
              type="number">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleSearchList()"
              size="small">
              筛选
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>影票</span>
    </el-card>
    <div class="table-container">
      <el-table ref="ticketTable"
                :data="list"
                style="width: 100%;"
                :default-sort="{prop:'createTime'}"
                v-loading="listLoading" border>
        <el-table-column label="序号" width="50" align="center">
          <template slot-scope="scope">{{
              ((listQuery.pageNum - 1) * listQuery.pageSize) + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column label="订单编号" prop="uuid" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.uuid }}</template>
        </el-table-column>
        <el-table-column prop="rowPieceId" label="排片 id" align="center">
          <template slot-scope="scope">{{ scope.row.rowPieceId }}</template>
        </el-table-column>
        <el-table-column label="座位编号" align="center">
          <template slot-scope="scope">{{ scope.row.seats }}</template>
        </el-table-column>
        <el-table-column label="折扣" align="center">
          <template slot-scope="scope">{{ scope.row.discount }}</template>
        </el-table-column>
        <el-table-column label="实付金额（￥）" align="center" width="140px">
          <template slot-scope="scope">{{ scope.row.costs }}</template>
        </el-table-column>
        <el-table-column label="下单时间" prop="createTime" align="center" width="200px">
          <template slot-scope="scope">{{ scope.row.createTime | formatDateTime  }}</template>
        </el-table-column>
        <el-table-column label="付款状态" align="center">
          <template slot-scope="scope">{{ scope.row.status }}</template>
        </el-table-column>
        <el-table-column label="使用状态" align="center">
          <template slot-scope="scope">{{ scope.row.checkStatus }}</template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="toFilm(scope.$index, scope.row)">
              查看影片
            </el-button>
            <el-button size="mini"
                       type="text"
                       v-if="scope.row.status==='已支付'"
                       @click="refundTicket(scope.$index, scope.row)">
              退票
            </el-button>
            <el-button size="mini"
                       type="text"
                       v-if="scope.row.status==='未支付'"
                       @click="paidTicket(scope.$index, scope.row)">
              付款
            </el-button>
            <!--            验证是否是管理员-->
            <el-button size="mini"
                       type="text"
                       v-if="isAdmin()"
                       @click="checkTicket(scope.$index, scope.row)">
              检票
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
// todo 接口
import {
  paidOrder,
  refundOrder,
  checkOrder,
  listTicketOrders
} from '@/api/order'
import {formatDate} from '@/utils/date';
import {mapGetters} from "vuex";

const defaultListQuery = {
  username: null,
  status: null,
  checkStatus: null,
  rowPieceId: null,
  pageNum: 1,
  pageSize: 10
};
export default {
  name: 'index',
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      statues: [{name: "未支付"}, {name: "已支付"}, {name: "已取消"}],
      checkStatues: [{name: "待使用"}, {name: "已使用"}, {name: "已退票"}],
      list: [],
      total: null,
      listLoading: false,
      isEdit: false
    }
  },
  created() {
    this.getList()
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return '';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  computed: {
    ...mapGetters([
      'username'
    ])
  },
  methods: {
    isAdmin() {
      return this.username === 'admin'
    },
    toFilm(index, row) {
      this.$router.push({
        name: 'film', params: {
          rowPieceId: row.rowPieceId
        }
      });
    },
    getList() {
      this.listLoading = true;
      listTicketOrders(this.listQuery).then(res => {
        this.listLoading = false;
        this.list = res.data.list;
        this.total = res.data.total;
      });
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    refundTicket(index, row) {
      refundOrder(row.uuid).then(res => {
        this.$message.success("退票成功");
      }).catch(e => {
        this.$message.error(e.message)
      });
    },
    checkTicket(index, row) {
      checkOrder(row.uuid).then(res => {
        this.$message.success("检票成功");
      }).catch(e => {
        this.$message.error(e.message)
      });
    },
    paidTicket(index, row) {
      paidOrder(row.uuid).then(res => {
        this.$message.success("支付成功");
      }).catch(e => {
        this.$message.error(e.message)
      });
    },
  }
}
</script>
<style></style>


